<?php
/*
  功能：历史中奖记录表 model
  作者：cxm
  时间：2019-10-23
*/

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use DateTimeInterface;

class AwardHistory extends BaseModel {
    use SoftDeletes;
    protected $dates = ['deleted_at']; //开启deleted_at
    protected $table='award_history';  //绑定表
    // 时间格式转换
    protected $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s'
    ];

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     * 领取信息转换
     */
    public function getDrawDataAttribute(){
        if (!isset($this->attributes['draw_data']) || empty($this->attributes['draw_data'])) {
            return [];
        }
        $draw_data = $this->attributes['draw_data'];
        $draw_data = json_decode($draw_data,true);
        return $draw_data;
    }

    /**
     * 用户
     */
    public function user(){
        return $this->hasOne(User::class,'id','user_id');
    }

    /**
     * 活动
     */
    public function activity(){
        return $this->hasOne(Activity::class,'id','activity_id');
    }

    /**
     * 中奖奖品
     */
    public function goods(){
        return $this->hasOne(AwardGoods::class,'id','award_id');
    }

    /**
     * 订单
     */
    public function order(){
        return $this->hasOne(Order::class,'id','order_id');
    }

    /**
     * 指定活动指定用户获奖情况
     * @param $user_id
     * @param $activity_id
     * @return mixed
     */
    public static function getUserDraw($user_id,$activity_id){
        $where = [];
        $where[] = ['user_id',$user_id];
        $where[] = ['activity_id',$activity_id];
        return self::where($where)->first();
    }

    /**
     * 指定用户是否获得次奖
     * @param $user_id 用户ID
     * @param $goods_id 奖品ID
     * @return mixed
     */
    public static function userGetNum($user_id,$goods_id){
        $where = [];
        $where[] = ['user_id',$user_id];
        $where[] = ['award_id',$goods_id];
        return self::where($where)->count();
    }

    /**
     * 指定商品中奖次数
     * @param $goods_id 奖品ID
     * @return mixed
     */
    public static function getDrawNum($goods_id){
        return self::where('award_id',$goods_id)->count();
    }

    /**
     * 用户中奖信息
     * @param $user_id 用户ID
     * @param $activity_id 活动ID
     * @return mixed
     */
    public static function userPrizeInfo($user_id,$activity_id){
        return  self::where('user_id',$user_id)
            ->where('activity_id',$activity_id)
            ->whereHas('goods',function($query){
                $query->where('is_prize',2); // 已中奖的
            })->first();
    }

    /**
     * 计算指定活动的领取数量
     * @param $activity_id 活动ID
     * @param $start_time 开始时间
     * @param $end_time 结束时间
     * @param boolean status 是否领取
     * @return mixed
     */
    public static function statisticsByTime($activity_id,$start_time,$end_time,$status=false){
        $result = self::where('activity_id',$activity_id)
            ->whereBetween('order.created_at', [$start_time, $end_time]);
        if ($status == true) {
            $result->where('status',1);// 已兑奖
        }
        return $result->count();
    }

    /**
     * 计算指定时间的奖品数量
     * @param $award_id 奖品ID
     * @param $start_time 开始时间
     * @param $end_time 结束时间
     * @param boolean status 是否领取
     * @return mixed
     */
    public static function statisticsAwardsByTime($award_id,$start_time,$end_time,$status=false){
        $result = self::where('award_id',$award_id)
            ->whereBetween('order.created_at', [$start_time, $end_time]);
        if ($status == true) {
            $result->where('status',1);// 已兑奖
        }
        return $result->count();
    }

    /**
     * 用户当天是否参与抽奖
     * @param $user_id 用户ID
     * @param $activity_id 活动ID
     * @return mixed
     */
    public static function userDayPartake($user_id,$activity_id){
        return self::where('user_id',$user_id)
                ->where('activity_id',$activity_id)
                ->whereDate('created_at',date('Y-m-d'))
                ->first();
    }
}